package com.lingyu.jsjx.service.impl;

import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lingyu.jsjx.domain.CoursePlan;
import com.lingyu.jsjx.domain.UserDocument;
import com.lingyu.jsjx.domain.dto.TeachingReflectionDTO;
import com.lingyu.jsjx.mapper.UserDocumentMapper;
import com.lingyu.jsjx.service.IUserDocumentService;
import com.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.bean.BeanValidators;
import com.ruoyi.common.datascope.annotation.DataScope;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.RemoteDeptService;
import com.ruoyi.system.api.domain.SysDept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lingyu.jsjx.mapper.TeachingReflectionMapper;
import com.lingyu.jsjx.domain.TeachingReflection;
import com.lingyu.jsjx.service.ITeachingReflectionService;

import javax.validation.Validator;

/**
 * 教学反思Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-07-31
 */
@Service
public class TeachingReflectionServiceImpl extends ServiceImpl<TeachingReflectionMapper, TeachingReflection> implements ITeachingReflectionService
{
    @Autowired
    private TeachingReflectionMapper teachingReflectionMapper;
    @Autowired
    private RemoteDeptService deptService;
    @Autowired
    private Validator validator;

    /**
     * 查询教学反思
     * 
     * @param reflectionId 教学反思主键
     * @return 教学反思
     */
    @Override
    public TeachingReflection selectTeachingReflectionByReflectionId(Long reflectionId)
    {
        return teachingReflectionMapper.selectTeachingReflectionByReflectionId(reflectionId);
    }

    /**
     * 查询教学反思列表
     * 
     * @param teachingReflectionDTO 教学反思
     * @return 教学反思
     */
    @Override
    @DataScope(deptAlias = "u", userAlias = "tr",type = "jsjx")
    public List<TeachingReflection> selectTeachingReflectionList(TeachingReflectionDTO teachingReflectionDTO)
    {
        return teachingReflectionMapper.selectTeachingReflectionList(teachingReflectionDTO);
    }

    /**
     * 新增教学反思
     * 
     * @param teachingReflection 教学反思
     * @return 结果
     */
    @Override
    public int insertTeachingReflection(TeachingReflection teachingReflection)
    {
        return baseMapper.insert(teachingReflection);
    }

    /**
     * 修改教学反思
     * 
     * @param teachingReflection 教学反思
     * @return 结果
     */
    @Override
    public int updateTeachingReflection(TeachingReflection teachingReflection)
    {
        return baseMapper.updateById(teachingReflection);
    }

    /**
     * 批量删除教学反思
     * 
     * @param reflectionIds 需要删除的教学反思主键
     * @return 结果
     */
    @Override
    public int deleteTeachingReflectionByReflectionIds(Long[] reflectionIds)
    {
        return teachingReflectionMapper.deleteTeachingReflectionByReflectionIds(reflectionIds);
    }

    /**
     * 删除教学反思信息
     * 
     * @param reflectionId 教学反思主键
     * @return 结果
     */
    @Override
    public int deleteTeachingReflectionByReflectionId(Long reflectionId)
    {
        return teachingReflectionMapper.deleteTeachingReflectionByReflectionId(reflectionId);
    }

    /**
     * 导入教学反思数据
     * @param teachingReflectionList
     * @param isUpdateSupport
     * @return
     */
    @Override
    public String importTeachindReflection(List<TeachingReflection> teachingReflectionList, Boolean isUpdateSupport) {
        if (StringUtils.isNull(teachingReflectionList) || teachingReflectionList.size() == 0) {
            throw new ServiceException("导入数据不能为空！");
        }

        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();

        for (TeachingReflection teachingReflection : teachingReflectionList) {
            try {
                SysDept dept1 = deptService.selectDeptByName(teachingReflection.getCollageName(), 0L, SecurityConstants.INNER).getData();
                SysDept dept2 = null;
                SysDept dept3 = null;

                if (dept1 != null)
                    dept2 = deptService.selectDeptByName(teachingReflection.getMajorName(), dept1.getDeptId(), SecurityConstants.INNER).getData();
                if (dept2 != null)
                    dept3 = deptService.selectDeptByName(teachingReflection.getClazzName(), dept2.getDeptId(), SecurityConstants.INNER).getData();
                if (dept1 != null) {
                    teachingReflection.setCollageId(dept1.getDeptId());
                }
                if (dept2 != null) {
                    teachingReflection.setMajorId(dept2.getDeptId());
                }

                if (dept3 != null) {
                    teachingReflection.setClazzId(dept3.getDeptId());
                }
                teachingReflection.setTeacherId(SecurityUtils.getUserId());
                teachingReflection.setTeacherName(SecurityUtils.getUsername());
                teachingReflection.setCreateBy(String.valueOf(SecurityUtils.getUserId()));
                teachingReflection.setCreateByName(SecurityUtils.getUsername());
                teachingReflection.setCreateTime(DateUtils.getNowDate());

                // 验证数据合法性
                BeanValidators.validateWithException(validator, teachingReflection);
                teachingReflectionMapper.insert(teachingReflection);
                successNum++;
                successMsg.append("<br/>" + successNum + "、教学反思课程名称为 " + teachingReflection.getCourseName() + " 导入成功");
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "、教学反思课程名称为 " + teachingReflection.getCourseName() + " 导入失败：";
                failureMsg.append(msg).append(e.getMessage());
                log.error(msg, e);
            }
        }

        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }

        return successMsg.toString();
    }
}
